﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Logic.BaseContent;
using Job.Entity;
using Job.Logic.Personal;
using Job.Logic.Company;

namespace Job.Web
{
    public partial class Item : BasePage
    {
        int subjectId = myR["subjectid"].IntValue;

        int contentId = myR["contentid"].IntValue;

        int categoryId = myR["categoryid"].IntValue;

        string jobNo = myR["jobno"].StringValue;

        int intCategoryId = 0, intSubjectId = 0, intContentId = 0;

        private void AdjustParameter()
        {
            //根据content_id来查询subject_id以及category_id
            if (contentId > 0 && subjectId <= 0)
            {
                BaseContentLogic logic = new BaseContentLogic();
                JobBaseContent info = logic.GetContentByContentId(contentId);
                if (null != info)
                {
                    intContentId = info.ContentId;
                    intSubjectId = info.SubjectId.Value;
                    BaseInfo subject = WebCommon.GetSubjectInfo(intSubjectId);
                    intCategoryId = subject.CatagoryId;
                }
            }
            else if (subjectId > 0 && categoryId <= 0)  //根据subject_id来得到category_id
            {
                intSubjectId = subjectId;
                BaseInfo subject = WebCommon.GetSubjectInfo(intSubjectId);
                intCategoryId = subject.CatagoryId;
            }
            else
            {
                intCategoryId = categoryId;
                intSubjectId = subjectId;
                intContentId = contentId;
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            AdjustParameter();
            LoadNavControl();
            LoadMainControl();

            if (categoryId == (Int32)Constants.CategoryType.Company)
            {
                lbCategoryName.Text = "关于无忧";
            }
            else if (categoryId == (Int32)Constants.CategoryType.Hunter)
            {
                lbCategoryName.Text = "猎头专区";
            }
            else if (categoryId == (Int32)Constants.CategoryType.Service)
            {
                lbCategoryName.Text = "人事外包";
            }

            BaseInfo subjectInfo = WebCommon.GetSubjectInfo(subjectId);
            lbSubjectName.Text = subjectInfo.DisplayTitle;
        }

        private void LoadNavControl()
        {
            string ctrlPath = "~/userControls/Public/SubjectNavgate.ascx";
            if (intContentId > 0)
            {
                BaseInfo info = WebCommon.GetSubjectInfo(intSubjectId);
                if (info.IsList)
                    ctrlPath = "~/userControls/Public/ContentDetailLeft.ascx";  //如果Subject为List并且指定了某一条记录时
                else
                    ctrlPath = "~/userControls/Public/SubjectNavgate.ascx";    
            }
            else if(intSubjectId > 0 )
            {                                
                //如果是二级栏目下的信息或list页面就加载导航的二级栏目，
                ctrlPath = "~/userControls/Public/SubjectNavgate.ascx";                
            }
            this.LoadCtrl(ctrlPath, "phNav");
        }

        private void LoadMainControl()
        {
            string ctrlPath = "";
            if (intSubjectId == (int)Constants.SubjectType.HunterPositionList)
            {
                if (jobNo == null || jobNo.Trim().Equals(""))
                {
                    ctrlPath = "~/userControls/Hunter/HunterJobList.ascx";
                    //string newsPath = "~/userControls/Public/BottomNews.ascx";
                    //this.LoadCtrl(newsPath, "phBottomNews");
                }
                else
                {
                    ctrlPath = "~/userControls/Hunter/HunterJobItem.ascx";
                    //this.phBottomNews.Visible = false;
                }
            }
            else
            {
                ctrlPath = "~/userControls/Public/ContentDetail.ascx";
                if (intContentId > 0)
                {
                    ctrlPath = "~/userControls/Public/ContentDetail.ascx";
                }
                else if (intSubjectId > 0)
                {
                    bool isList = WebCommon.GetSubjectInfo(intSubjectId.ToString()).IsList;
                    if (isList)
                    {
                        ctrlPath = "~/userControls/Public/SubjectList.ascx";
                        //string newsPath = "~/userControls/Public/BottomNews.ascx";
                        //this.LoadCtrl(newsPath, "phBottomNews");
                    }
                    else
                    {
                        ctrlPath = "~/userControls/Public/ContentDetail.ascx";
                        //this.phBottomNews.Visible = false;
                    }
                }
            }

            if (ctrlPath != "")
                this.LoadCtrl(ctrlPath, "phMain");
        }


        protected void btUserLogin_Click(object sender, ImageClickEventArgs e)
        {
            PersonalLogic logic = new PersonalLogic();
            JobPersonAccount account = logic.Login(tbUserName.Text.Trim());
            if (null == account)
            {
                showAlert("登录帐号不存在！");
                return;
            }

            if (account.Password.Equals(tbUserPassword.Text.Trim()))
            {
                logic.UpdateLogin(account.PersonAccountId);
                SessionPersonal spersonal = new SessionPersonal()
                {
                    LoginName = tbUserName.Text.Trim(),
                    PersonAccountid = account.PersonAccountId,
                    UserName = account.UserName
                };

                MySession session = new MySession();
                session.AddPersonal2Session(spersonal);

                Response.Redirect(WebCommon.GetApplicationPath(Request) + "Person/PersonManager.aspx?categoryid=1&ctrlid=2");
            }
            else
            {
                showAlert("用户名或密码错误！");
            }

        }

        private void showAlert(String message)
        {
            this.msgTable.Visible = true;
            this.message.InnerHtml = "<strong>" + message + "</strong>";
        }

        protected void btCompangLogin_Click(object sender, ImageClickEventArgs e)
        {
            CompanyLogic logic = new CompanyLogic();
            JobCompanyInfo company = logic.GetCompanyByLogin(tbCompanyName.Text.Trim());
            if (null == company)
            {
                showAlert("登录帐号不存在！");
                return;
            }

            if (company.LoginPswd.Equals(tbCompanyPassword.Text))
            {
                SessionCompany sCompany = new SessionCompany();
                sCompany.CompanyId = company.CompanyInfoId;
                sCompany.CompanyName = company.CompanyName;

                MySession session = new MySession();
                session.AddCompany2Session(sCompany);

                Response.Redirect(WebCommon.GetApplicationPath(Request) + "Company/CompanyManager.aspx?categoryid=2&subjectid=2");
                return;
            }
            else
            {
                showAlert("用户名或密码错误！");
            }

        }
    }
}
